/*
 * SettingsDialog.java
 *
 * Created on March 23, 2008, 1:58 PM
 */

package be.belgium.eid.beidgui;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author  kristofoverdulve
 */
public class SettingsDialog extends javax.swing.JDialog {
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    
    public static String PREFERENCES = "preferences.xml";
    
    private boolean fVerifyOCSP = false;
    
    private boolean fVerifyCRL = false;
    
    /** Creates new form SettingsDialog */
    public SettingsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();	
	loadProperties();
    }
    
    public boolean getVerifyOCSP() {
	return fVerifyOCSP;
    }
    
    public boolean getVerifyCRL() {
	return fVerifyCRL;
    }
        
    private void loadProperties() {
	try {
	    Properties prop = new Properties();
	    prop.loadFromXML(new FileInputStream(PREFERENCES));
	    fVerifyOCSP = Boolean.valueOf(prop.getProperty("OCSP"));
	    fVerifyCRL = Boolean.valueOf(prop.getProperty("CRL"));
	   
	    if (fVerifyOCSP) {
		fOCSPYes.setSelected(true);
	    } else {
		fOCSPNo.setSelected(true);
	    }
	    
	    if (fVerifyCRL) {
		fCRLYes.setSelected(true);
	    } else {
		fCRLNo.setSelected(true);
	    }
	} catch (Exception ex) {
	    fVerifyOCSP = false;
	    fVerifyCRL = false;
	}
    }
    
    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        languageButtonGroup = new javax.swing.ButtonGroup();
        ocspButtonGroup = new javax.swing.ButtonGroup();
        crlButtonGroup = new javax.swing.ButtonGroup();
        fOkButton = new javax.swing.JButton();
        fCancelButton = new javax.swing.JButton();
        fPreferencesPanel = new javax.swing.JTabbedPane();
        fLanguageMainPanel = new javax.swing.JPanel();
        fLanguagePanel = new javax.swing.JPanel();
        fLanguageEnglish = new javax.swing.JRadioButton();
        fLanguageDutch = new javax.swing.JRadioButton();
        fLanguageFrench = new javax.swing.JRadioButton();
        fLanguageGerman = new javax.swing.JRadioButton();
        fCertificateValidationMainPanel = new javax.swing.JPanel();
        fOCSPPanel = new javax.swing.JPanel();
        fOCSPYes = new javax.swing.JRadioButton();
        fOCSPNo = new javax.swing.JRadioButton();
        fCRLPanel = new javax.swing.JPanel();
        fCRLYes = new javax.swing.JRadioButton();
        fCRLNo = new javax.swing.JRadioButton();

        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(be.belgium.eid.beidgui.BEIDGuiApp.class).getContext().getResourceMap(SettingsDialog.class);
        fOkButton.setText(resourceMap.getString("fOkButton.text")); // NOI18N
        fOkButton.setName("fOkButton"); // NOI18N
        fOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fOkButtonActionPerformed(evt);
            }
        });

        fCancelButton.setText(resourceMap.getString("fCancelButton.text")); // NOI18N
        fCancelButton.setName("fCancelButton"); // NOI18N
        fCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fCancelButtonActionPerformed(evt);
            }
        });

        fPreferencesPanel.setName("fPreferencesPanel"); // NOI18N

        fLanguageMainPanel.setName("fLanguageMainPanel"); // NOI18N

        fLanguagePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("fLanguagePanel.border.title"))); // NOI18N
        fLanguagePanel.setName("fLanguagePanel"); // NOI18N

        languageButtonGroup.add(fLanguageEnglish);
        fLanguageEnglish.setSelected(true);
        fLanguageEnglish.setText(resourceMap.getString("fLanguageEnglish.text")); // NOI18N
        fLanguageEnglish.setEnabled(false);
        fLanguageEnglish.setName("fLanguageEnglish"); // NOI18N

        languageButtonGroup.add(fLanguageDutch);
        fLanguageDutch.setText(resourceMap.getString("fLanguageDutch.text")); // NOI18N
        fLanguageDutch.setEnabled(false);
        fLanguageDutch.setName("fLanguageDutch"); // NOI18N

        languageButtonGroup.add(fLanguageFrench);
        fLanguageFrench.setText(resourceMap.getString("fLanguageFrench.text")); // NOI18N
        fLanguageFrench.setEnabled(false);
        fLanguageFrench.setName("fLanguageFrench"); // NOI18N

        languageButtonGroup.add(fLanguageGerman);
        fLanguageGerman.setText(resourceMap.getString("fLanguageGerman.text")); // NOI18N
        fLanguageGerman.setEnabled(false);
        fLanguageGerman.setName("fLanguageGerman"); // NOI18N

        org.jdesktop.layout.GroupLayout fLanguagePanelLayout = new org.jdesktop.layout.GroupLayout(fLanguagePanel);
        fLanguagePanel.setLayout(fLanguagePanelLayout);
        fLanguagePanelLayout.setHorizontalGroup(
            fLanguagePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fLanguagePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(fLanguagePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(fLanguageEnglish)
                    .add(fLanguageDutch)
                    .add(fLanguageFrench)
                    .add(fLanguageGerman))
                .addContainerGap(275, Short.MAX_VALUE))
        );
        fLanguagePanelLayout.setVerticalGroup(
            fLanguagePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fLanguagePanelLayout.createSequentialGroup()
                .add(fLanguageEnglish)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fLanguageDutch)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fLanguageFrench)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fLanguageGerman)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout fLanguageMainPanelLayout = new org.jdesktop.layout.GroupLayout(fLanguageMainPanel);
        fLanguageMainPanel.setLayout(fLanguageMainPanelLayout);
        fLanguageMainPanelLayout.setHorizontalGroup(
            fLanguageMainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fLanguageMainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(fLanguagePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        fLanguageMainPanelLayout.setVerticalGroup(
            fLanguageMainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fLanguageMainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(fLanguagePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(67, Short.MAX_VALUE))
        );

        fPreferencesPanel.addTab(resourceMap.getString("fLanguageMainPanel.TabConstraints.tabTitle"), fLanguageMainPanel); // NOI18N

        fCertificateValidationMainPanel.setName("fCertificateValidationMainPanel"); // NOI18N

        fOCSPPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("fOCSPPanel.border.title"))); // NOI18N
        fOCSPPanel.setName("fOCSPPanel"); // NOI18N

        ocspButtonGroup.add(fOCSPYes);
        fOCSPYes.setText(resourceMap.getString("fOCSPYes.text")); // NOI18N
        fOCSPYes.setName("fOCSPYes"); // NOI18N

        ocspButtonGroup.add(fOCSPNo);
        fOCSPNo.setText(resourceMap.getString("fOCSPNo.text")); // NOI18N
        fOCSPNo.setName("fOCSPNo"); // NOI18N

        org.jdesktop.layout.GroupLayout fOCSPPanelLayout = new org.jdesktop.layout.GroupLayout(fOCSPPanel);
        fOCSPPanel.setLayout(fOCSPPanelLayout);
        fOCSPPanelLayout.setHorizontalGroup(
            fOCSPPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fOCSPPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(fOCSPPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(fOCSPYes)
                    .add(fOCSPNo))
                .addContainerGap(301, Short.MAX_VALUE))
        );
        fOCSPPanelLayout.setVerticalGroup(
            fOCSPPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fOCSPPanelLayout.createSequentialGroup()
                .add(fOCSPYes)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fOCSPNo))
        );

        fCRLPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("fCRLPanel.border.title"))); // NOI18N
        fCRLPanel.setName("fCRLPanel"); // NOI18N

        crlButtonGroup.add(fCRLYes);
        fCRLYes.setText(resourceMap.getString("fCRLYes.text")); // NOI18N
        fCRLYes.setName("fCRLYes"); // NOI18N

        crlButtonGroup.add(fCRLNo);
        fCRLNo.setText(resourceMap.getString("fCRLNo.text")); // NOI18N
        fCRLNo.setName("fCRLNo"); // NOI18N

        org.jdesktop.layout.GroupLayout fCRLPanelLayout = new org.jdesktop.layout.GroupLayout(fCRLPanel);
        fCRLPanel.setLayout(fCRLPanelLayout);
        fCRLPanelLayout.setHorizontalGroup(
            fCRLPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fCRLPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(fCRLPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(fCRLYes)
                    .add(fCRLNo))
                .addContainerGap(301, Short.MAX_VALUE))
        );
        fCRLPanelLayout.setVerticalGroup(
            fCRLPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fCRLPanelLayout.createSequentialGroup()
                .add(fCRLYes)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fCRLNo))
        );

        org.jdesktop.layout.GroupLayout fCertificateValidationMainPanelLayout = new org.jdesktop.layout.GroupLayout(fCertificateValidationMainPanel);
        fCertificateValidationMainPanel.setLayout(fCertificateValidationMainPanelLayout);
        fCertificateValidationMainPanelLayout.setHorizontalGroup(
            fCertificateValidationMainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, fCertificateValidationMainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(fCertificateValidationMainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, fCRLPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, fOCSPPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        fCertificateValidationMainPanelLayout.setVerticalGroup(
            fCertificateValidationMainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fCertificateValidationMainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(fOCSPPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(fCRLPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(41, Short.MAX_VALUE))
        );

        fPreferencesPanel.addTab(resourceMap.getString("fCertificateValidationMainPanel.TabConstraints.tabTitle"), fCertificateValidationMainPanel); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(250, Short.MAX_VALUE)
                .add(fOkButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fCancelButton)
                .addContainerGap())
            .add(fPreferencesPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
        );

        layout.linkSize(new java.awt.Component[] {fCancelButton, fOkButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(fPreferencesPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 286, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(fCancelButton)
                    .add(fOkButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void fOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fOkButtonActionPerformed
	try {
	    Properties prop = new Properties();
	    prop.setProperty("OCSP", Boolean.toString(fOCSPYes.isSelected()));
	    prop.setProperty("CRL", Boolean.toString(fCRLYes.isSelected()));
	    prop.storeToXML(new FileOutputStream(PREFERENCES), null);

	    doClose(RET_OK);
	} catch (IOException ex) {//GEN-LAST:event_fOkButtonActionPerformed
	    Logger.getLogger(SettingsDialog.class.getName()).log(Level.SEVERE, null, ex);
	}
}                                         
    
    private void fCancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fCancelButtonActionPerformed
        doClose(RET_CANCEL);
}//GEN-LAST:event_fCancelButtonActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog
    
    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SettingsDialog dialog = new SettingsDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup crlButtonGroup;
    private javax.swing.JRadioButton fCRLNo;
    private javax.swing.JPanel fCRLPanel;
    private javax.swing.JRadioButton fCRLYes;
    private javax.swing.JButton fCancelButton;
    private javax.swing.JPanel fCertificateValidationMainPanel;
    private javax.swing.JRadioButton fLanguageDutch;
    private javax.swing.JRadioButton fLanguageEnglish;
    private javax.swing.JRadioButton fLanguageFrench;
    private javax.swing.JRadioButton fLanguageGerman;
    private javax.swing.JPanel fLanguageMainPanel;
    private javax.swing.JPanel fLanguagePanel;
    private javax.swing.JRadioButton fOCSPNo;
    private javax.swing.JPanel fOCSPPanel;
    private javax.swing.JRadioButton fOCSPYes;
    private javax.swing.JButton fOkButton;
    private javax.swing.JTabbedPane fPreferencesPanel;
    private javax.swing.ButtonGroup languageButtonGroup;
    private javax.swing.ButtonGroup ocspButtonGroup;
    // End of variables declaration//GEN-END:variables
    
    private int returnStatus = RET_CANCEL;
}
